export default{
    state: {
        list: JSON.parse(localStorage.getItem('vuex-todos')) || [],
    type:'all'
    },
    mutations:{
        selDone(state,id){
           const todo = state.list.find(item => item.id === id)
           todo.isDone = !todo.isDone
        },
        delOne(state,id){
            state.list = state.list.filter(item => item.id !== id)
        },
        add(state,name){
            state.list.unshift({
                id: +new Date(),
                name,
                isDone:false
            })
        },
        check(state,val){
            return state.list.forEach(item => item.isDone = val)
        },
        clearDone(state){
            state.list = state.list.filter(item => !item.isDone)
        },
        sel(state,type){
            state.type = type
        },
    },
    getters:{
        leftCount(state){
            return state.list.filter(item => !item.isDone).length
        },
        rightCount(state){
            return state.list.some(item => item.isDone)
        },
        showList(state){
            if(state.type === 'yes'){
                return state.list.filter(item => item.isDone)
            }else if(state.type === 'no'){
                return state.list.filter(item => !item.isDone)
            }else{
                return state.list
            }
        }
    },
    actions:{
        clearAsyncDone(context){
            setTimeout(() => {
                context.commit('clearDone')
            },1000)
        }
    },
    // 开启命名空间
    namespaced: true
}